package com.we.wecc.ut.mid.service;

import com.we.wecc.us.db.mapper.UserMapper;
import com.we.wecc.us.db.mapper.UserTickerLogMapper;
import com.we.wecc.us.db.mapper.UserTickerMapper;
import com.we.wecc.us.db.model.User;
import com.we.wecc.us.db.model.UserTicker;
import com.we.wecc.us.db.model.UserTickerExample;
import com.we.wecc.us.db.model.UserTickerLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 币service
 *
 * @author Cheney
 *
 */
@Service
public class TickerService {

    @Resource
    UserTickerMapper userTickerMapper;

    @Resource
    UserTickerLogMapper userTickerLogMapper;

    @Resource
    UserMapper userMapper;


    public UserTickerLog addTicker(Long userId, String cid, Double num, String reason) {
        return addTicker(userId, cid, num, reason, null);
    }

    public UserTickerLog addTicker(Long userId, String cid, Double num, String reason, Long otherUserId) {

        return addTicker(userId, cid, num, reason, otherUserId, "已完成", null, null, null);
    }

    /**
     * 增加用户钱包
     *
     * @param userId
     * @param cid
     * @param num
     * @param reason
     *            原因
     */
    public UserTickerLog addTicker(Long userId, String cid, Double num, String reason, Long otherUserId, String state, String billNo, Double fee, String address) {
        UserTicker ut = new UserTicker();
        UserTickerExample ex = new UserTickerExample();
        ex.or().andCidEqualTo(cid).andUserIdEqualTo(userId);

        Double cashing = null;
        if (reason.contains("提现") && !reason.contains("竞猜") && !reason.contains("失败"))
            cashing = num;

        List<UserTicker> userTickers = userTickerMapper.selectByExample(ex);
        if(userTickers.isEmpty()){
            return null;
        }

        UserTicker exist = userTickers.get(0);
        if (exist != null) {
            exist.setNum(exist.getNum() + num);
            exist.setCashing(cashing);
            userTickerMapper.updateByPrimaryKeySelective(exist);

        } else {
            ut.setNum(num);
            ut.setCashing(cashing);
            userTickerMapper.insertSelective(ut);
        }

        // 添加日志
        if (state == null)
            state = "已完成";
        UserTickerLog log = new UserTickerLog();
        log.setCid(cid);
        log.setNum(num);
        // 设置手续费
        if (fee == null)
            log.setFee(fee);

        log.setReason(reason);
        log.setUserId(userId);
        try {
            User user = userMapper.selectByPrimaryKey(userId);
            log.setUserName(user.getName());
            log.setUserTel(user.getTel());
        } catch (Exception e) {
        }
        log.setOtherUserId(otherUserId);
        log.setState(state);
        log.setBillNo(billNo);
        log.setAddress(address);
        userTickerLogMapper.insertSelective(log);

        return log;
    }

    /**
     * 获取用户指定币的钱包
     *
     * @param userId
     * @param cid
     */
    public UserTicker getUserTicker(Long userId, String cid) {
        UserTicker ut = new UserTicker();
        ut.setCid(cid);
        ut.setUserId(userId);

        UserTickerExample ex = new UserTickerExample();
        ex.or().andCidEqualTo(cid).andUserIdEqualTo(userId);

        List<UserTicker> userTickers = userTickerMapper.selectByExample(ex);
        if(userTickers.isEmpty()){
            return null;
        }

        UserTicker exist = userTickers.get(0);

        if (exist == null) {
            ut.setNum(0d);
            userTickerMapper.insertSelective(ut);
            return ut;
        }
        return exist;
    }
}
